package com.itheima.extendedHomework.test3;

import java.util.ArrayList;
import java.util.Scanner;

/*
3.n个数围成一个圈，从第一个人开始报数，数到3的人退出圈子，然后再从1开始报数，
	依次类推，问数到还剩最后一个人的时候是第几个人
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("请输入一个整数N");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        ArrayList<Integer> arrayList = new ArrayList<>();

        for (int i = 1; i < input+1 ; i++) {
            arrayList.add(i);
        }
        System.out.println(arrayList);

        int num = 0;//累加的数
        int temp = 0;//用于修正集合删掉的元素
        int can = 0;//殘留的值，数组遍历完之后余几就为几
        int can1 = 0;
        while(true){
            num++;
            if (num%3 == 0 && temp == 0){
                can = num;
                arrayList.remove(num-1);
                temp++;
                continue;
            }else if (num%3 == 0 && temp != 0){
                can = num;
                arrayList.remove(num-temp-1);
                temp++;
                continue;
            }
            if (num-temp > arrayList.size()){
                temp = 1;//復位
                can1 = num - can - 1;
                num = can1;
                continue;
            }
            if (arrayList.size() == 1){
                break;
            }
        }
        System.out.println(arrayList);
    }
}
